package com.wy2cloud.core.date;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;


/**
 * 时间二次封装
 * @author wyy
 *@version 2015-11-11
 */
public class DateTime {

	//Logger log= LogManager.getLogger(getClass());
	
	/**
	 * 默认日期格式
	 */
	private final static String DefaultFormat="yyyy-MM-dd HH:mm:ss";
	
	public static DateTime Now(){
		return new DateTime();
	}
	
	/**
	 * 字符串格式化为date
	 * @param date
	 * @param format
	 * @return
	 * @throws ParseException 
	 */
	public static DateTime Parse(String date, String format) throws ParseException{
		Date d=new SimpleDateFormat(format).parse(date);
		return new DateTime(d);
	}
	
	/**
	 * 字符串格式化为date ,formart:DefaultFormat
	 * @param date
	 * @return
	 * @throws ParseException 
	 */
	public static DateTime Parse(String date) throws ParseException{
		return Parse(date,DefaultFormat);
	}
	
	public static DateTime Parse(long time){
		return new DateTime(time);
	}
	
	/**
	 * 日历对象
	 */
	private Calendar calendar;
	
	public DateTime(){
		this.calendar=Calendar.getInstance();
	}
	public DateTime(Date date){
		this.calendar=Calendar.getInstance();
		this.calendar.setTime(date);
	}
	public DateTime(long time){
		this.calendar=Calendar.getInstance();
		this.calendar.setTimeInMillis(time);
	}
	
	public DateTime(int year, int month, int dayOfMonth, int hourOfDay,
            int minute, int second){
		this.calendar=new GregorianCalendar(year, month, dayOfMonth,hourOfDay,minute,second);
	}
	public DateTime(int year, int month, int dayOfMonth){
		this.calendar=new GregorianCalendar(year, month, dayOfMonth);
	}
	
	@Override
	public String toString() {
		return toString("yyyy-MM-dd HH:mm:ss");
	}
	
	/**
	 * 转换为字符串
	 * @param format
	 * @return
	 */
	public String toString(String format) {
		Date date=this.calendar.getTime();
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}
	
	public long getTime(){
		return this.calendar.getTimeInMillis();
	}
	
	public Date getDate(){
		return this.calendar.getTime();
	}
	
	public Timestamp getTimestamp(){
		return new Timestamp(this.getTime());
	}
	
	public Integer getYear(){
		return this.calendar.get(Calendar.YEAR);   
	}
	
	public Integer getMonth(){
		return this.calendar.get(Calendar.MONTH);   
	}
	/**
	 * 返回星期几枚�?
	 * @return
	 */
	public DayOfWeek getDayOfWeek(){
		//返回星期数字�?1:星期天，2:星期�?
		Integer index= this.calendar.get(Calendar.DAY_OF_WEEK);
		return DayOfWeek.Parse(index);
	}
	public Integer getDay(){
		return this.calendar.get(Calendar.DAY_OF_MONTH);
	}
	
	public Integer getHour(){
		return this.calendar.get(Calendar.HOUR_OF_DAY);
	}
	public Integer getMinute(){
		return this.calendar.get(Calendar.MINUTE);
	}
	public Integer getSecond(){
		return this.calendar.get(Calendar.SECOND);
	}
	
	
	public DateTime addYear(Integer amount){
		this.calendar.add(Calendar.YEAR, amount);
		return this;
	}
	public DateTime addMonth(Integer amount){
		this.calendar.add(Calendar.MONTH, amount);
		return this;
	}
	/*public DateTime addWeek(Integer amount){
		this.calendar.add(Calendar.DAY_OF_WEEK, amount);
		return this;
	}*/
	public DateTime addDay(Integer amount){
		this.calendar.add(Calendar.DAY_OF_MONTH, amount);
		return this;
	}
	public DateTime addHour(Integer amount){
		this.calendar.add(Calendar.HOUR_OF_DAY, amount);
		return this;
	}
	public DateTime addMinute(Integer amount){
		this.calendar.add(Calendar.MINUTE, amount);
		return this;
	}
	public DateTime addSecond(Integer amount){
		this.calendar.add(Calendar.SECOND, amount);
		return this;
	}
	
	/**
	 * 减去Datetime,返回时间�?
	 * @param dt
	 * @return
	 */
	public TimeSpan Substract(DateTime dt){
		return new TimeSpan(this.getTime()-dt.getTime());
	}
}
